#ifndef SQL_ACTION_H
#define SQL_ACTION_H

#include <pqxx/pqxx>
#include <vector>
#include <deque>

struct DB_StudentInf
{
  DB_StudentInf(const long & id_,const std::string & name_)
    : ID(id_)
    , Name(name_)
  {
  }
  long ID;
  std::string Name;
};

struct DB_TaskInf
{
  DB_TaskInf(const long & id_,const std::string & name_,const std::string & createTime_)
    : id(id_),name(name_),createTime(createTime_)
  {}
  long id;
  std::string name;
  std::string createTime;
};

struct DB_RecordInf
{
  long rec_id;
  long stu_id;
  long task_id;
  std::string create_time;
  bool smpl_status;
  bool record_finish;
};

class SQLAction
{
  public:
    SQLAction(std::string user,std::string passwd,std::string host,std::string database); virtual ~SQLAction();
    std::deque<DB_StudentInf> GetStudentInf();
    std::string GetStudentInf(long id);
    void CreateStudent(int id,std::string Name); //失败抛出异常
    void DeleteStudent(int id);
    bool CheckStudentExists();
    long CreateTask(std::string name);
    void DeleteTask(int id);
    std::deque<DB_TaskInf> GetTaskInf();
    /*
     * Record API结构
     * 开始录制 -----  停止录制
     * CreateRecord ---- RecordFinish
     * 
     * 为了解决录制未完成情况，可以对数据进行垃圾回收和错误规避
     * */
    long CreateRecord(long stu_id,long task_id);
    void RecordFinish(long id);
    void DeleteRecord(long id);
    std::deque<long> GetRecordIDList(long task_id);
    std::deque<long> GetHandleIDList();
    std::deque<long> GetUnhandleIDList();
    std::vector<long> GetHandleIDListWithTask(long task_id);
    std::tuple<long,long,std::string,bool,bool> GetRecordInf(long id);
    std::vector<long> GetTaskList();
    bool CheckStudentExists(long id);
    void SetHandleFinish(long id);
  private:
    pqxx::connection DBconn;
    pqxx::work DBwork;
};

#endif
